﻿using AbpDemo.Articles.Dto;
using CSRedis;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AbpDemo
{
    public class SendEmailService : BackgroundService
    {
        private readonly CSRedisClient client;
        private readonly ILogger<SendEmailService> logger;

        public SendEmailService(CSRedisClient client, ILogger<SendEmailService> logger)
        {
            this.client = client;
            this.logger = logger;
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            /*Thread thread = new Thread(async () =>
            {
                while (true)
                {
                    string emaildto = await client.RPopAsync($"Article:EmailDto");
                    if (!string.IsNullOrWhiteSpace(emaildto))
                    {
                        EmailDto emailDto = JsonConvert.DeserializeObject<EmailDto>(emaildto);
                        //发送邮件
                        Thread.Sleep(1000);
                        Console.WriteLine(emailDto.Email);
                    }
                }
            });

            thread.IsBackground = true;
            thread.Start();*/

            client.Subscribe(("channel.1", msg => {
                var body = msg.Body;
                logger.LogInformation($"订阅日志：{body}");
            }   
            ));
        }
    }
}
